package com.example.wwk.graduationpractice.Broadcast;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.util.Log;


import com.example.wwk.graduationpractice.Fragment.MainActivity;
import com.example.wwk.graduationpractice.Globalutils.AppForeOrBackGround;
import com.example.wwk.graduationpractice.Globalutils.MyApplication;
import com.example.wwk.graduationpractice.R;


public class NotifyComeBroadReceiver extends BroadcastReceiver {
    /*
    实现程序后台运行，接收消息的提醒通知
    */
    NotificationManager manager;
    public static final String NOTIFY_MSG_COME =  "com.example.wwk.graduationpractice.NOTIFY_MSG_COME";
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        String action = intent.getAction();
        String from = intent.getStringExtra("from");
        String body = intent.getStringExtra("body");
        //判读action是否符合过滤条件以及是否不再前台
        if (action.equals(NOTIFY_MSG_COME)&&!AppForeOrBackGround.isAppForeground(context)){
            try {

                 manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

//                int size = 200;
                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.login_logo2);
                NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
                builder.setContentTitle("心咨");
                builder.setContentText(from + ": " + body);
                builder.setWhen(System.currentTimeMillis());
                builder.setSmallIcon(R.drawable.d_baibai);
                builder.setLargeIcon(bitmap);
                builder.setAutoCancel(true);  //当点击后自动消失
                Intent intent1 = new Intent(context, MainActivity.class);
                PendingIntent pi = PendingIntent.getActivity(context, 0, intent1, 0);
                builder.setContentIntent(pi);//跳转

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                    VISIBILITY_PUBLIC 只有在没有锁屏时会显示通知
//                    VISIBILITY_PRIVATE 任何情况都会显示通知
//                    VISIBILITY_SECRET 在安全锁和没有锁屏的情况下显示通知
                    builder.setVisibility(Notification.VISIBILITY_PRIVATE);
                    //设置点击跳转
                    Intent hangIntent = new Intent();
                    PendingIntent hangPendingIntent = PendingIntent.getActivity(context, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
                    builder.setFullScreenIntent(hangPendingIntent, true);
                }

                Notification notification = builder.build();
                notification.defaults |= Notification.DEFAULT_SOUND;//默认声音提示
                manager.notify(1, notification);
                new Handler().postDelayed(new Runnable() {     //Handler实现延迟刷新UI
                    @Override
                    public void run() {
                        manager.cancel(1);          //通知栏显示后消失
                    }
                }, 3000);        //秒


            } catch (UnsupportedOperationException e) {
                e.printStackTrace();
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
        }

    }
}
